/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package treegen;

import java.util.ArrayList;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
 * Класс листа дерева, содержит тип, размер и цвет листьев
 *
 * @author Дарья
 */
public class Leaf {

    /**
     * Размер листа
     */
    public static String SIZE = "Oтсутствует";
    /**
     * Тип листа
     */
    public static String SHAPE = "Oтсутствует";
    /**
     * Цвет листа
     */
    public static String COLOR = "Oтсутствует";
    /**
     * Точка присоединения листа к ветви
     */
    private static Point2D POINT = null;
    /**
     * Коэффициент масштабирования листа
     */
    private static double K;
    /**
     * Картинка листа
     */
    public static ImageView IMAGE = null;
    /**
     * Контейнер листов ветви
     */
    public static ArrayList<ImageView> LEAFS = new ArrayList();
    /**
     * Ширина листа
     */
    public static double WIDTH_LEAF;
    /**
     * Высота листа
     */
    public static double HEIGHT_LEAF;

    /**
     * Конструктор, иницилизирующий лист дерева
     *
     * @see Leaf
     * @param point точка на ветви
     * @param wBranch ширина ветви
     * @param typeBranch тип ветви
     */
    public Leaf(Point2D point, double wBranch, int typeBranch) {
        double rand;
        if (typeBranch == 1) {//горизонтальная ветвь
            do {
                rand = point.getY() + (Math.random() * (wBranch));
            } while (rand == 0);
            POINT = new Point2D(point.getX(), rand);
        } else {//вертикальная ветвь
            do {
                rand = point.getX() + (Math.random() * (wBranch));
            } while (rand == 0);
            POINT = new Point2D(rand, point.getY());
        }
    }

    /**
     * Установить коэффициент масштабирования листа
     */
    public static void setFactor() {
        if (SIZE.contains("Крупные(20-40 см)")) {
            K = 1.5;
        } else if (SIZE.contains("Средние (до 20 см)")) {
            K = 1;
        } else if (SIZE.contains("Малые (до 10 см)")) {
            K = 0.8;
        }
    }

    /**
     * Добавить лист в контейнер листов ветви
     */
    public static void addLeaf() {

        Image im = IMAGE.getImage();
        ImageView imv = new ImageView(im);
        imv.setFitWidth(WIDTH_LEAF);
        imv.setFitHeight(HEIGHT_LEAF);
        imv.setX(POINT.getX());
        imv.setY(POINT.getY());
        double rand = (Math.random() * 360.0);
        imv.setRotate(rand);
        LEAFS.add(imv);
    }

    /**
     * Отрисовать листья из контейнера листов ветви
     *
     * @param branch ветвь
     */
    public static void paint(Group branch) {
        branch.getChildren().addAll(LEAFS);
    }

    /**
     * Загрузить картинку листа по параметрам типа и цвета
     */
    public static void loadImage() {
        if (Leaf.SHAPE.contains("Oтсутствует")) {
            Leaf.SIZE = "Oтсутствует";
            Leaf.IMAGE = null;
            Leaf.COLOR = "Oтсутствует";
        } else if (Leaf.SHAPE.contains("Игольчатая")) {
            if (Leaf.COLOR.contains("Зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/igolka_green.png")));
            } else if (Leaf.COLOR.contains("Темно-зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/igolka_dark_green.png")));
            } else if (Leaf.COLOR.contains("Оранжевый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/igolka_orange.png")));
            } else if (Leaf.COLOR.contains("Жёлтый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/igolka_yellow.png")));
            } else if (Leaf.COLOR.contains("Пурпурный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/igolka_red.png")));
            }
        } else if (Leaf.SHAPE.contains("Линейная")) {
            if (Leaf.COLOR.contains("Зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/line_green.png")));
            } else if (Leaf.COLOR.contains("Темно-зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/line_dark_green.png")));
            } else if (Leaf.COLOR.contains("Оранжевый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/line_orange.png")));
            } else if (Leaf.COLOR.contains("Жёлтый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/line_yellow.png")));
            } else if (Leaf.COLOR.contains("Пурпурный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/line_red.png")));
            }
        } else if (Leaf.SHAPE.contains("Ланцетная")) {
            if (Leaf.COLOR.contains("Зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/lanceolate_green.png")));
            } else if (Leaf.COLOR.contains("Темно-зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/lanceolate_dark_green.png")));
            } else if (Leaf.COLOR.contains("Оранжевый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/lanceolate_orange.png")));
            } else if (Leaf.COLOR.contains("Жёлтый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/lanceolate_yellow.png")));
            } else if (Leaf.COLOR.contains("Пурпурный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/lanceolate_red.png")));
            }
        } else if (Leaf.SHAPE.contains("Овальная")) {
            if (Leaf.COLOR.contains("Зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/oval_green.png")));
            } else if (Leaf.COLOR.contains("Темно-зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/oval_dark_green.png")));
            } else if (Leaf.COLOR.contains("Оранжевый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/oval_orange.png")));
            } else if (Leaf.COLOR.contains("Жёлтый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/oval_yellow.png")));
            } else if (Leaf.COLOR.contains("Пурпурный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/oval_red.png")));
            }
        } else if (Leaf.SHAPE.contains("Округлая")) {
            if (Leaf.COLOR.contains("Зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/roundish_green.png")));
            } else if (Leaf.COLOR.contains("Темно-зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/roundish_dark_green.png")));
            } else if (Leaf.COLOR.contains("Оранжевый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/roundish_orange.png")));
            } else if (Leaf.COLOR.contains("Жёлтый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/roundish_yellow.png")));
            } else if (Leaf.COLOR.contains("Пурпурный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/roundish_red.png")));
            }
        } else if (Leaf.SHAPE.contains("Сердцевидная")) {
            if (Leaf.COLOR.contains("Зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/cordate_green.png")));
            } else if (Leaf.COLOR.contains("Темно-зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/cordate_dark_green.png")));
            } else if (Leaf.COLOR.contains("Оранжевый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/cordate_orange.png")));
            } else if (Leaf.COLOR.contains("Жёлтый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/cordate_yellow.png")));
            } else if (Leaf.COLOR.contains("Пурпурный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/cordate_red.png")));
            }
        } else if (Leaf.SHAPE.contains("Дельтовидная")) {
            if (Leaf.COLOR.contains("Зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/deltoid_green.png")));
            } else if (Leaf.COLOR.contains("Темно-зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/deltoid_dark_green.png")));
            } else if (Leaf.COLOR.contains("Оранжевый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/deltoid_orange.png")));
            } else if (Leaf.COLOR.contains("Жёлтый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/deltoid_yellow.png")));
            } else if (Leaf.COLOR.contains("Пурпурный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/deltoid_red.png")));
            }
        } else if (Leaf.SHAPE.contains("Обратнояйцевидная")) {
            if (Leaf.COLOR.contains("Зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/obovate_green.png")));
            } else if (Leaf.COLOR.contains("Темно-зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/obovate_dark_green.png")));
            } else if (Leaf.COLOR.contains("Оранжевый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/obovate_orange.png")));
            } else if (Leaf.COLOR.contains("Жёлтый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/obovate_yellow.png")));
            } else if (Leaf.COLOR.contains("Пурпурный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/obovate_red.png")));
            }
        } else if (Leaf.SHAPE.contains("Ромбовидная")) {
            if (Leaf.COLOR.contains("Зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/rhomboid_green.png")));
            } else if (Leaf.COLOR.contains("Темно-зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/rhomboid_dark_green.png")));
            } else if (Leaf.COLOR.contains("Оранжевый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/rhomboid_orange.png")));
            } else if (Leaf.COLOR.contains("Жёлтый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/rhomboid_yellow.png")));
            } else if (Leaf.COLOR.contains("Пурпурный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/rhomboid_red.png")));
            }
        } else if (Leaf.SHAPE.contains("Заострённая")) {
            if (Leaf.COLOR.contains("Зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/pointed_green.png")));
            } else if (Leaf.COLOR.contains("Темно-зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/pointed_dark_green.png")));
            } else if (Leaf.COLOR.contains("Оранжевый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/pointed_orange.png")));
            } else if (Leaf.COLOR.contains("Жёлтый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/pointed_yellow.png")));
            } else if (Leaf.COLOR.contains("Пурпурный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/pointed_red.png")));
            }
        } else if (Leaf.SHAPE.contains("Веерообразная")) {
            if (Leaf.COLOR.contains("Зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/harp_green.png")));
            } else if (Leaf.COLOR.contains("Темно-зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/harp_dark_green.png")));
            } else if (Leaf.COLOR.contains("Оранжевый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/harp_orange.png")));
            } else if (Leaf.COLOR.contains("Жёлтый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/harp_yellow.png")));
            } else if (Leaf.COLOR.contains("Пурпурный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/harp_red.png")));
            }
        } else if (Leaf.SHAPE.contains("Дланевидная")) {
            if (Leaf.COLOR.contains("Зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/palmate_green.png")));
            } else if (Leaf.COLOR.contains("Темно-зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/palmate_dark_green.png")));
            } else if (Leaf.COLOR.contains("Оранжевый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/palmate_orange.png")));
            } else if (Leaf.COLOR.contains("Жёлтый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/palmate_yellow.png")));
            } else if (Leaf.COLOR.contains("Пурпурный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/palmate_red.png")));
            }
        } else if (Leaf.SHAPE.contains("Почковидная")) {
            if (Leaf.COLOR.contains("Зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/nodular_green.png")));
            } else if (Leaf.COLOR.contains("Темно-зелёный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/nodular_dark_green.png")));
            } else if (Leaf.COLOR.contains("Оранжевый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/nodular_orange.png")));
            } else if (Leaf.COLOR.contains("Жёлтый")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/nodular_yellow.png")));
            } else if (Leaf.COLOR.contains("Пурпурный")) {
                Leaf.IMAGE = new ImageView(new Image(LeafPanel.class.getResourceAsStream("pictures/nodular_red.png")));
            }
        }
    }

    /**
     * Установить размер листа
     *
     * @param w ширина листа
     * @param h высота листа
     */
    public static void setSize(double w, double h) {
        Leaf.WIDTH_LEAF = w * K;
        Leaf.HEIGHT_LEAF = h * K;
    }
}
